$(function () {
    var $datagrid = $("#datagrid"), $toolbar = $("#toolbar"), $dialog = $("#dialog");
    $("#saveCity").on("click", function () {
        openDialog();
    });
    $("#updateCity").on("click", function () {
        var rows = $datagrid.datagrid("getSelected");
        if (rows) {
            var url = "form.jsp?name=" + rows.NameCn + "&sequence=" + rows.Sequence + "&cityId=" + rows.Id;
            openDialog(url);
        } else {
            Constant.showMessage("请选择目标！");
        }
    });
    $datagrid.datagrid({
        singleSelect: true,
        fit: true,
        border: false,
        fitColumns: true,
        url: Constant.ctx("/mulanweb/common/city/search"),
        method: 'post',
        pagination: true,
        toolbar: $toolbar,
        columns: [[
            {field: 'Id', title: '编号'},
            {field: 'NameCn', title: '名称'},
            {field: 'Sequence', title: '排序'},
            {field: 'CreateTime', title: '注册时间'},
            {
                field: 'DeleteTime', title: "操作", formatter: function (v, r, i) {
                if (v) {
                    return '<a href="#" onclick="cityDelete(' + r.Id + ')">撤销</a>';
                } else {
                    return '<a href="#" onclick="cityDelete(' + r.Id + ')">删除</a>';
                }
            }
            }
        ]],
        onBeforeLoad: function (param) {
            param.PageNumber = param.page;
            param.PageSize = param.rows;

            var cityName = $("#searchCity").val();
            if (cityName) {
                param.cityName = cityName;
            }
            return true;
        },
        loadFilter: function (data) {
            var newData = {};
            if (data.ReturnCode == 1) {
                newData.rows = data.DataList;
                if (newData.rows.length > 0) {
                    newData.total = data.TotalRow;
                } else {
                    newData.total = 0;
                }
            }
            return newData;
        }
    });
    function saveAccount() {
        $("form").form("submit", {
            url: $("form").attr("action"),
            onSubmit: function () {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    $.messager.progress('close');	// hide progress bar while the form is invalid
                }
                return isValid;
            },
            success: function (respText) {
                var resp = $.parseJSON(respText);
                if (resp.ReturnCode == 1) {
                    $("#datagrid").datagrid("reload");
                    $dialog.dialog("close");
                } else if (resp.AlertMessage.indexOf("此地点已存在") != -1) {
                    $.messager.confirm('提示', "此地点已被删除,是否需要恢复？", function (r) {
                        if (r) {
                            var cityId = resp.AlertMessage.split(":")[1];
                            $.post(Constant.ctx("/mulanweb/admin/city/reset"), {cityId: cityId}, function (info) {
                                Constant.showMessage(info.AlertMessage);
                                if (info.ReturnCode == 1) {
                                    $("#datagrid").datagrid("reload");
                                    $dialog.dialog("close");
                                }
                            })
                        }
                    });
                }
            }
        });
    }

    function openDialog(url) {
        $dialog.dialog({
            title: url?"编辑地点":"新增地点",
            width: 300,
            height: 200,
            closed: false,
            cache: false,
            href: url || 'form.jsp',
            modal: true,
            onClose: function () {
                $("#form-dialog").dialog("destroy");
            },
            buttons: [{
                text: '保存',
                handler: saveAccount
            }]
        });
    }
})

function cityDelete(targetId) {
    var para = {
        cityId: targetId
    }

    $.post(Constant.ctx("/mulanweb/admin/city/delete"), para, function (info) {
        if (info.ReturnCode == 1) {
            Constant.showMessage(info.AlertMessage, "操作成功");
            $("#datagrid").datagrid("reload");
        } else {
            Constant.showMessage(info.message, "操作失败");
        }
    })
}

function doSearch() {
    $("#datagrid").datagrid("reload");
}